package com.laowang.serviceImpl;

import com.laowang.dataoject.ProductInfo;
import com.laowang.dto.CartDTO;
import com.laowang.enums.ProductStatusEnum;
import com.laowang.enums.ResultEnum;
import com.laowang.exception.SellException;
import com.laowang.repository.ProductInfoRepository;
import com.laowang.sercice.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

/**
 * Created by Administrator on 2019/1/9.
 */
@Service
public class ProductInfoServiceImpl implements ProductService{
    @Autowired
    private ProductInfoRepository productInfoRepository;


    @Override
    public Optional<ProductInfo> findById(String productId) {

        return productInfoRepository.findById(productId);
    }

    @Override
    public List<ProductInfo> findUpAllProduct() {
        return productInfoRepository.findByProductStatus(ProductStatusEnum.up.getCode());
    }

    @Override
    public Page<ProductInfo> findAllProduct(Pageable pageable) {
        return productInfoRepository.findAll(pageable);
    }

    @Override
    public ProductInfo saveProduct(ProductInfo productInfo) {

        return productInfoRepository.save(productInfo);
    }

    @Override
    @Transactional
    public void increaseStock(List<CartDTO> cartDTOList) {
        for (CartDTO cartDTO:cartDTOList){
            if (cartDTO==null){
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIT);
            }
            Optional<ProductInfo> productInfo1 =
                    productInfoRepository.findById(cartDTO.getProductId());
            ProductInfo productInfo = productInfo1.get();
            Integer i = productInfo.getProductStock() + cartDTO.getProductQuantity();
            productInfo.setProductStock(i);
            productInfoRepository.save(productInfo);

        }


    }

    @Override
    @Transactional
    public void decreaseStock(List<CartDTO> cartDTOList) {
        for (CartDTO cartDto:cartDTOList
             ) {
            Optional<ProductInfo> productInfo1
                    = productInfoRepository.findById(cartDto.getProductId());
            if (cartDto==null){
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIT);
            }
           Integer result =  productInfo1.get().getProductStock()-cartDto.getProductQuantity();
            if (result<0){
                throw new SellException(ResultEnum.PRODUCT_NOT_ENOUGH  );
            }
            ProductInfo productInfo = productInfo1.get();
            productInfo.setProductStock(result);
            productInfoRepository.save(productInfo);
        }

    }

    @Override
    public ProductInfo onSell(String productId) {
        Optional<ProductInfo> productInfo1 = productInfoRepository.findById(productId);
        ProductInfo productInfo = productInfo1.get();
        if(productInfo==null){
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIT);
        }
        if(productInfo.getProductStatus()==ProductStatusEnum.up.getCode()){
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }
        productInfo.setProductStatus(ProductStatusEnum.up.getCode());

        return productInfoRepository.save(productInfo);
    }

    @Override
    public ProductInfo offSell(String productId) {
        Optional<ProductInfo> productInfo1 = productInfoRepository.findById(productId);
        ProductInfo productInfo = productInfo1.get();
        if(productInfo==null){
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIT);
        }
        if(productInfo.getProductStatus()==ProductStatusEnum.down.getCode()){
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }
        productInfo.setProductStatus(ProductStatusEnum.down.getCode());

        return productInfoRepository.save(productInfo);
    }
}
